package com.white.camlib;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;
import android.view.ViewGroup;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * @author ytf
 * Created by on 2021/06/04 10:26
 */
public class CameraEngine {

    private static CameraEngine instance;

    public static CameraEngine getInstance(){
        if(instance == null){
            synchronized(CameraEngine.class){
                if(instance == null){
                    instance = new CameraEngine();
                }
            }
        }
        return instance;
    }

    private volatile AtomicBoolean flag = new AtomicBoolean(false);

    private Context mContext;
    private CameraService mService;

    private ServiceConnection connection = new ServiceConnection() {

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            mService = ((CameraService.MyBinder)service).getService();
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

    public void init(Context ctx){
        mContext = ctx;
        if(!flag.getAndSet(true)){
            mContext.startService(new Intent(mContext, CameraService.class));
            mContext.bindService(new Intent(mContext, CameraService.class), connection, Context.BIND_AUTO_CREATE);
        }
    }

    /**
     * attach到指定viewgroup进行预览显示
     * @param parent   viewgroup
     * @param withFrameCallback  buffercallback 用于人脸识别，不需要可传空
     */
    public void attachParent2Display(ViewGroup parent, CameraKit.FrameCallback withFrameCallback, CameraKit.TextureAttachCallback...attachCallbacks){
        if(mService != null){
            mService.attach(parent, false, 1024, 600, 0, withFrameCallback, attachCallbacks);
        }
    }

    /**
     * attach到指定viewgroup进行预览显示
     * @param parent   viewgroup
     * @param withFrameCallback  buffercallback 用于人脸识别，不需要可传空
     */
    public void attachParent2Display(ViewGroup parent,
                                     boolean autoFit, int wRatio, int hRatio,
                                     int screenRotation,
                                     CameraKit.FrameCallback withFrameCallback, CameraKit.TextureAttachCallback...attachCallbacks){
        if(mService != null){
            mService.attach(parent, autoFit, wRatio, hRatio, screenRotation, withFrameCallback, attachCallbacks);
        }
    }

    private ExecutorService pool = Executors.newSingleThreadExecutor();
    private Handler mHandler = new Handler(Looper.getMainLooper());

    public void takePicture(String path, final CameraKit.PictureCallback callback){
        if(mService != null){
            mService.takePicture(path, new CameraKit.PictureCallback() {
                @Override
                public void onTakePicture(final String path) {
                    mHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            callback.onTakePicture(path);
                        }
                    });
                }
            }, pool);
        }
    }

    public void detach(){
        if(mService != null){
            mService.detach();
        }
    }

    public void shutdown(){
        if(flag.getAndSet(false)){
            mContext.unbindService(connection);
            mContext.stopService(new Intent(mContext, CameraService.class));
        }
    }
}
